﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FrozenBubble.Screens
{
    public class HighScoresScreen : GameScreen
    {
        private const int InitialHighPosX = 90;
        private const int InitialHighPosY = 68;

        private Texture2D _background;
        private Texture2D _frame;
        private SpriteFont _font;

        private InputState _oldInput;

        private List<HighScorePosition> _positions;

        public HighScoresScreen()
        {
            _positions = new List<HighScorePosition>();
        }

        public override void LoadContent()
        {
            _background = Game.Content.Load<Texture2D>("Graphics/back_hiscores");
            _frame = Game.Content.Load<Texture2D>("Graphics/hiscore_frame");
            _font = Game.Content.Load<SpriteFont>("Fonts/font-hi");

            LoadHighScores();
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Draw(_background, ScreenManager.ViewportRectangle, Color.White);

            // Draw High Scores
            foreach (HighScorePosition pos in _positions)
            {
                SpriteBatch.Draw(_frame, pos.framePosition, Color.White);
                SpriteBatch.DrawString(_font, pos.score.PlayerName, pos.namePosition, Color.White);
                if (pos.score.HasWon)
                    SpriteBatch.DrawString(_font, "WON!", pos.levelPosition, Color.White);
                else
                    SpriteBatch.DrawString(_font, "LVL-" + pos.score.Level, pos.levelPosition, Color.White);
                SpriteBatch.DrawString(_font, pos.timeText, pos.timePosition, Color.White);
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!otherScreenHasFocus)
            {
                InputState input = Input.State;
                if (!input.IsActionActive(InputAction.End) && _oldInput.IsActionActive(InputAction.End))
                    ExitScreen();

                _oldInput = input;
            }
        }

        // in this method we precalculate all the positions
        private void LoadHighScores()
        {
            int x = InitialHighPosX;
            int y = InitialHighPosY;

            foreach(HighScore score in Storage.HighScores)
            {
                HighScorePosition pos = new HighScorePosition();
                pos.framePosition = new Vector2(x - 7, y - 6);
                pos.namePosition = CalculateCenteredPosition(new Vector2(x, y), score.PlayerName);
                if (score.HasWon)
                    pos.levelPosition = CalculateCenteredPosition(new Vector2(x, y + 20), "WON!");
                else
                    pos.levelPosition = CalculateCenteredPosition(new Vector2(x, y + 20), "LVL-" + score.Level);
                pos.score = score;

                int min = score.TotalPlayTime / 60;
                int sec = score.TotalPlayTime - min * 60;
                string timeText = min + "'" + ((sec < 10) ? "0" : "") + sec + "''";

                pos.timeText = timeText;
                pos.timePosition = CalculateCenteredPosition(new Vector2(x, y + 40), timeText);

                _positions.Add(pos);

                x += 98;
                if (x > 550)
                {
                    x = InitialHighPosX;
                    y += 175;
                }

                if (y > 440)
                    break;
            }
        }

        private Vector2 CalculateCenteredPosition(Vector2 position, string text)
        {
            float x = position.X + (_frame.Width - _font.MeasureString(text).X) / 2 - 6;
            float y = position.Y + _frame.Height - 8;

            return new Vector2(x, y);
        }
    }

    public struct HighScorePosition
    {
        public Vector2 framePosition;
        public Vector2 namePosition;
        public Vector2 levelPosition;
        public Vector2 imagePosition;
        public Vector2 timePosition;
        public HighScore score;
        public string timeText;
    }
}
